//
//  KBRefreshControl.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/23.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

class KBRefreshControl: UIRefreshControl {
    
    // MARK: - 属性
    private let refreshControlOffsetY: CGFloat = -60
    /// 标识箭头是否朝上，默认朝上
    private var isUp = true
    
    /// 覆盖父类的属性，监听下拉动作
    override var frame: CGRect {
        didSet {
            if frame.origin.y > 0 {
                return
            }

            //判断是否刷新(是否拖拽显示了refreshControl)
            if refreshing {
                //显示菊花并且旋转
                refreshView.startRotation()
            }
            
            // 下拉小于-60，且箭头是朝下
            if frame.origin.y < refreshControlOffsetY && !isUp {
                isUp = !isUp
                // 箭头动画朝上
                refreshView.rotationTipIcon(isUp)
                
                // 下拉大于-60，且箭头是朝上
            } else if frame.origin.y > refreshControlOffsetY && isUp {
                isUp = !isUp
                // 箭头动画朝下
                refreshView.rotationTipIcon(isUp)
            }
        }
    }
    
    // MARK: - 方法
    override init() {
        super.init()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func endRefreshing() {
        super.endRefreshing()
        
        refreshView.endRefresh()
    }
    
    private func setupUI() {
        addSubview(refreshView)
        //添加约束
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
    }
    
    // MARK: - 懒加载
    private lazy var refreshView: KBRefreshView = KBRefreshView.refreshView()
    
}



class KBRefreshView: UIView {
    
    // MARK: - 属性
    @IBOutlet weak var tipIcon: UIImageView!
  
    @IBOutlet weak var tipView: UIView!
    
    @IBOutlet weak var circleView: UIImageView!
    
    // MARK: - 方法
    /// 返回xib中的refreshView
    class func refreshView() -> KBRefreshView {
        return NSBundle.mainBundle().loadNibNamed("KBRefreshView", owner: nil, options: nil).last as! KBRefreshView
    }
    
    /// 箭头动画旋转
    func rotationTipIcon(isUp: Bool) {
        UIView.animateWithDuration(0.25) { () -> Void in
            self.tipIcon.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI) - 0.001) : CGAffineTransformIdentity
        }
    }
    
    //显示菊花并且旋转
    func startRotation() {
        
        let animationKey = "rotationCircleView"
        // 如果已存在动画，直接返回
        if circleView.layer.animationForKey(animationKey) != nil {
            return
        }
        
        tipView.hidden = true
        
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        
        animate.toValue = 2 * M_PI
        animate.duration = 0.25
        animate.repeatCount = MAXFLOAT
        //防止跳到其他控制器再跳回来，动画停止
        animate.removedOnCompletion = false
        
        circleView.layer.addAnimation(animate, forKey: animationKey)
    }
    
    /// 结束菊花旋转，显示箭头
    func endRefresh() {
        tipView.hidden = false
        
        circleView.layer.removeAllAnimations()
    }
}



